package start.spring.basic.flowable.config;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import start.spring.basic.flowable.common.ProcessConstants;
import start.spring.basic.util.common.StringUtil;

/**
 * 多实例处理类
 *
 * @author KonBAI
 */
@AllArgsConstructor
@Component("multiInstanceHandler")
public class MultiInstanceHandler {

    public Set<String> getUserIds(DelegateExecution execution) {
        Set<String> candidateUserIds = new LinkedHashSet<>();
        FlowElement flowElement = execution.getCurrentFlowElement();
        if (ObjectUtil.isNotEmpty(flowElement) && flowElement instanceof UserTask) {
            UserTask userTask = (UserTask) flowElement;
            Map<String, Object> variablesMap = execution.getVariables();
            //上一环节完成审批时设置当前多梳理环节审批人
            String userIds = String.valueOf(
            			variablesMap.get(ProcessConstants.USER_TYPE_ASSIGNEE + userTask.getId()));
            if(!StringUtil.isEmpty(userIds)) {
            	String[] userIdArray = userIds.split(",");
            	for(String userId:userIdArray) {
            		candidateUserIds.add(userId);
            	}
            }
        }
        return candidateUserIds;
    }
}
